package com.ptszyxx.popose.module.main.home.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.enums.ChatCallEnum;
import com.tencent.liteav.trtccalling.zhiyu.event.CallMatchEvent;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YStringUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class HomeCallMatchVM extends BaseViewModel<CommonRepository> {
    public ChatCallEnum callEnum;

    public HomeCallMatchVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onCallEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCancelCommand = new BindingCommand(()-> cancelMatch());

    public Disposable disposable;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(CallMatchEvent.class)
                .subscribe(new Consumer<CallMatchEvent>() {
                    @Override
                    public void accept(CallMatchEvent event){
                        if (event != null){
                            YActivityUtil.getInstance().close(HomeCallMatchVM.this);
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
    }

    //=========================================接口：start===========================================
    public void requestCallMatch(String matchType){
        Map<String, String> map = new HashMap<>();
        map.put("leixing", matchType);//1发起匹配 2取消匹配
        if (callEnum != null){
            map.put("type", callEnum == ChatCallEnum.AUDIO?"1": "2");//1语音匹配 2视频匹配
        }
        HttpUtils.getInstance()
                .data(model.callMatch(map), this,
                        new OnSuccessResult<String>() {
                            @Override
                            public void onSuccessResult(BaseResponse<String> response) {
                                if (YStringUtil.eq(2, matchType)){
                                    YActivityUtil.getInstance().close(HomeCallMatchVM.this);
                                }else{
                                    uc.onCallEvent.setValue(response.getData());
                                }
                            }
                        });
    }

    private void cancelMatch() {
        requestCallMatch("2");
    }


}
